package c2.advanced.business.common;

import c2.advanced.business.AdvancedBody;
import c2.advanced.business.base.AdvancedModule;
import c2.advanced.business.base.AdvancedSubModule;
import c2.advanced.business.common.mail.MailPostBox;
import c2.advanced.share.DbFacade;
import c2.advanced.business.base.KvData;
import cate.common.game.mail.MailCmd;
import cate.game.framework.GameException;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/** 通用服务总模块 */
@Slf4j
public class CommonModule extends AdvancedModule<CommonStatus> {

    private MailPostBox mailPostBox = new MailPostBox();

    private AdvancedBody context;
    public CommonModule(AdvancedBody context) {
        this.context = context;
    }
    @Override
    protected AdvancedBody context() {
        return context;
    }
    @Override
    protected DbFacade _db() {
        return context.dbBase();
    }

    @Override
    protected AdvancedSubModule[] getSubModules() {
        return new AdvancedSubModule[] { mailPostBox };
    }

    @Override
    protected String getStatusKey() {
        return KvData.KEY_DEMO;
    }

    @Override
    protected Class<CommonStatus> getStatusClass() {
        return CommonStatus.class;
    }

    @Override
    public String getName() {
        return "通用";
    }


    @Override
    protected int getIntervalSeconds() {
        return 1;
    }

    public void sendMail(MailCmd cmd) {
        if(cmd.srvId <= 0) {
            throw new GameException().content("cmd.srvId你是不是忘记填了？");
        }
        cmd.createTime = context.time.now();
        this.mailPostBox.add(cmd);
    }

    public void sendMails(List<MailCmd> cmds) {
        final long now = context.time.now();
        cmds.stream().forEach(cmd -> {
            if(cmd.srvId <= 0) {
                throw new GameException().content("cmd.srvId你是不是忘记填了？");
            }
            cmd.createTime = now;
        });
        this.mailPostBox.addList(cmds);
    }
}
